﻿(function ($) {
    app.modals.CreateOrEditAlcCodeModal = function () {
        var _modalManager;

        var _alcCodeService = abp.services.app.alcCode;

        //$(".maxlength-handler").maxlength({
        //    limitReachedClass: "label label-danger",
        //    alwaysShow: true,
        //    threshold: 5,
        //    placement: 'bottom'
        //});

        var _$alcCodeInformationForm = null;

        this.init = function (modalManager) {
            _modalManager = modalManager;
            _$alcCodeInformationForm = _modalManager.getModal().find("form[name=alcCodeInformationsForm]");

            //渲染select
            $("#cartypeIdEdit").selectpicker();
            $("#styleIdEdit").selectpicker();
        }

        //下拉框二级联动
        $("#cartypeIdEdit").change(function () {
            //获取工厂的ID
            var factoryID = $("#cartypeIdEdit").val();
            //获取车间
            var lst = $("#styleIdEdit");
            $.ajax({
                type: "get",
                url: "/Styles/GetAllStyleByCartypeId",
                data: { id: factoryID },
                dataType: 'JSON', //返回数据的格式
                contentType: 'application/json',
                success: function (data, status) {
                    if (status == "success") {
                        lst.empty();//清空下拉框
                        $.each(data, function (i, item) {
                            lst.append("<option value='" + data[i].Value + "'>" + data[i].DisplayText + "</option>");
                        });
                        lst.selectpicker('refresh');
                    }
                },
                error: function () {
                    toastr.error('Error');
                },
            });
        });


        this.save = function () {
            if (!_$alcCodeInformationForm.valid()) {
                return;
            }
            //校验通过

            var alcCode = _$alcCodeInformationForm.serializeFormToObject();
            //  console.log(alcCode);

            _modalManager.setBusy(true);

            _alcCodeService.createOrUpdateAlcCodeAsync({
                alcCodeEditDto: alcCode
            }).done(function () {
                //提示信息
                abp.notify.info(app.localize('SavedSuccessfully'));
                //关闭窗体
                _modalManager.close();
                //信息保存成功后调用事件，刷新列表
                abp.event.trigger('app.createOrEditAlcCodeModalSaved');
            }).always(function () {
                _modalManager.setBusy(false);
            });
        }
    }
})(jQuery);